package io;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class PWDemo2 {
    public static void main(String[] args) throws FileNotFoundException {
        FileOutputStream fos = new FileOutputStream("pw1.txt");
        OutputStreamWriter osw =new OutputStreamWriter(fos, StandardCharsets.UTF_8);
        BufferedWriter bw = new BufferedWriter(osw);
        PrintWriter pw = new PrintWriter(bw,true);//一般都要加自动刷新
        Scanner scanner =new Scanner(System.in);
        System.out.println("请输入文字,并单独输入exit时自动退出");
        while (true){
            String line = scanner.nextLine();
            if ("exit".equals(line)) {
                System.out.println("输出完成!");
                break;
            }
            pw.println(line);
        }
            pw.close();

        /*
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入的文字:");
        PrintWriter pw = new PrintWriter("pw.txt");
        while (true){
            String line = scanner.nextLine();
            if("exit".equals(line)){
                break;
            }
            pw.print(line);
            pw.flush();//一定要冲一把,要不存在数组里面,无法实时刷新,需要全部结束才会出来
                       //但是这个无法自动刷新
        }
        pw.close();
         */
    }
}
